package com.sisgesbyte.dao.ventas;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sisgesbyte.dao.beanDao.EstructuraVentaRequest;
import com.sisgesbyte.dao.beanDao.FiltrosSQLBean;
import com.sisgesbyte.dao.utilDao.CreaFiltroSqlString;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.entity.ventas.EstructuraVentaEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantEstructuraVentaDaoImpl extends AbstractFacade<EstructuraVentaEntity> implements MantEstructuraVentaDao {

	@Inject
	private CreaFiltroSqlString creaFiltroSqlString;
	
	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}
	
	public MantEstructuraVentaDaoImpl(){
		super(EstructuraVentaEntity.class);
	}
	
	
	@Override
	public List<EstructuraVentaEntity> listaEstructuraVenta(
			FiltrosSQLBean cadenaFiltros) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT id, nombre, descripcion, nivel, orden, padre_id, active ");
		jpql.append("FROM ventas.estructura_venta ");
		// concatenamos el filtro
		if (cadenaFiltros != null) {
			StringBuilder filtro = new StringBuilder();
			filtro.append(creaFiltroSqlString.concatenar(cadenaFiltros));
			jpql.append(filtro.toString());
		}
		Query query = em.createNativeQuery(jpql.toString(), EstructuraVentaEntity.class);
		if (cadenaFiltros != null && cadenaFiltros.getListaValores() != null
				&& cadenaFiltros.getListaValores().size() > 0) {
			int num = 0;
			for (Object valor : cadenaFiltros.getListaValores()) {
				query.setParameter(++num, valor);
			}
		}
		List<EstructuraVentaEntity> ls = query.getResultList();

		return ls;
	}

	@Override
	public GenericEntity guardarEstructuraVenta(EstructuraVentaRequest request) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT *  from ventas.sp_registrar_estructura_venta(:in_nombre, :in_descripcion, :in_nivel, :in_orden, :in_padre_id, :in_active )");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_nombre", request.getNombre());
			query.setParameter("in_descripcion", request.getDescripcion());
			query.setParameter("in_nivel", request.getNivel());
			query.setParameter("in_orden", request.getOrden());
			query.setParameter("in_padre_id", request.getPadreId());
			query.setParameter("in_active", request.getActive());
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarEstructuraVenta(
			EstructuraVentaRequest request) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT *  from ventas.sp_actualizar_estructura_venta(:in_id, :in_nombre, :in_descripcion, :in_nivel, :in_orden, :in_padre_id, :in_active )");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_id", request.getId());	
			query.setParameter("in_nombre", request.getNombre());
			query.setParameter("in_descripcion", request.getDescripcion());
			query.setParameter("in_nivel", request.getNivel());
			query.setParameter("in_orden", request.getOrden());
			query.setParameter("in_padre_id", request.getPadreId());
			query.setParameter("in_active", request.getActive());
		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarEstructuraVenta(int codEstructuraVenta) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("SELECT *  from ventas.sp_eliminar_estructura_venta(:in_id)");
		Query query = em.createNativeQuery(jpql.toString(), GenericEntity.class);
			query.setParameter("in_id", codEstructuraVenta);	
		return (GenericEntity) query.getSingleResult();
	}

}
